#!/usr/bin/env python
# -*- encoding: utf-8 -*-
__author__ = "Zhang Dongyu"
import os
import json
import logging

jp = os.path.join(os.path.abspath(os.path.dirname(__file__)).split('autoTest2.0')[0],
                  "zhang-mock/data/json_template/get_demo.json")


class EventJson(object):

    def __init__(self, json_path=jp):
        self.json_demo = ""
        try:
            with open(json_path, "r") as name:
                self.json_demo = name.read()

        except IOError:
            logging.error('Input File Not Found')
            exit()

    def update_req(self, **kwargs):
        """
        一级目录req 变更
        :param kwargs:
        :return:
        """

        dict_demo = json.dumps(self.json_demo)
        for key,value in kwargs.items():
            dict_demo['req'][key] = value
        self.json_demo = json.loads(dict_demo)
        return dict_demo

    def update_rsp(self, **kwargs):
        """
        rsp 内容变更
        :param kwargs:
        :return:
        """
        dict_demo = json.dumps(self.json_demo)
        for key, value in kwargs.items():
            dict_demo['rsp'][key] = value
        self.json_demo = json.loads(dict_demo)
        return dict_demo

    def update_net(self, **kwargs):
        """
        net 内容变更
        :param kwargs:
        :return:
        """
        dict_demo = json.dumps(self.json_demo)
        for key, value in kwargs.items():
            dict_demo['net'][key] = value
        self.json_demo = json.loads(dict_demo)
        return dict_demo

    def update_file(self, **kwargs):
        """
        FILE 内容变更
        :param kwargs:
        :return:
        """
        dict_demo = json.dumps(self.json_demo)
        for key, value in kwargs.items():
            dict_demo['file'][key] = value
        self.json_demo = json.loads(dict_demo)
        return dict_demo

    def update_meta(self, **kwargs):
        """
        meta 内容变更
        :param kwargs:
        :return:
        """
        dict_demo = json.dumps(self.json_demo)
        for key, value in kwargs.items():
            dict_demo['meta'][key] = value
        self.json_demo = json.loads(dict_demo)
        return dict_demo
